package com.ruoyi.common.core.domain.model;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 设备信息
 *
 * @author ruoyi
 */
@Data
@Schema(description = "设备信息")
public class DeviceInfo implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @Schema(description = "用户ID")
    private Long userId;

    /**
     * 用户名
     */
    @Schema(description = "用户名")
    private String username;

    /**
     * 用户唯一标识
     */
    @Schema(description = "用户唯一标识")
    private String token;

    /**
     * 登录时间
     */
    @Schema(description = "登录时间")
    private Long loginTime;

    /**
     * 登录IP地址
     */
    @Schema(description = "登录IP地址")
    private String ipaddr;

    /**
     * 登录地点
     */
    @Schema(description = "登录地点")
    private String loginLocation;

    /**
     * 浏览器类型
     */
    @Schema(description = "浏览器类型")
    private String browser;

    /**
     * 操作系统
     */
    @Schema(description = "操作系统")
    private String os;

    /**
     * 从LoginUser创建DeviceInfo
     *
     * @param loginUser 登录用户信息
     * @return DeviceInfo对象
     */
    public static DeviceInfo fromLoginUser(LoginUser loginUser) {
        if (loginUser == null) {
            return null;
        }
        
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setUserId(loginUser.getUserId());
        deviceInfo.setUsername(loginUser.getUsername());
        deviceInfo.setToken(loginUser.getToken());
        deviceInfo.setLoginTime(loginUser.getLoginTime());
        deviceInfo.setIpaddr(loginUser.getIpaddr());
        deviceInfo.setLoginLocation(loginUser.getLoginLocation());
        deviceInfo.setBrowser(loginUser.getBrowser());
        deviceInfo.setOs(loginUser.getOs());
        
        return deviceInfo;
    }

    /**
     * 将DeviceInfo应用到LoginUser
     *
     * @param loginUser  目标LoginUser对象
     * @param deviceInfo 设备信息
     */
    public static void applyToLoginUser(LoginUser loginUser, DeviceInfo deviceInfo) {
        if (loginUser == null || deviceInfo == null) {
            return;
        }
        
        loginUser.setToken(deviceInfo.getToken());
        loginUser.setLoginTime(deviceInfo.getLoginTime());
        loginUser.setIpaddr(deviceInfo.getIpaddr());
        loginUser.setLoginLocation(deviceInfo.getLoginLocation());
        loginUser.setBrowser(deviceInfo.getBrowser());
        loginUser.setOs(deviceInfo.getOs());
    }
}
